<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-wait-widget" :style="getStyle('wrap')">
      <div class="left" :style="getStyle('head')" v-if="headerShown">
        {{ optionsInfo.headerName }}
      </div>
      <div class="right edit" :key="widgetKey">
        <template v-if="!!passPatientInfos && !!(passPatientInfos.length)">
          <vue-seamless-scroll
              ref="scroll"
              class="scroll"
              @ScrollEnd="handleEnd"
              :class-option="classOptionTips"
              :data="passPatientInfos"
          >
            <div class="inside-content" :style="getStyle('list')">
              <div class="patient" v-for="(item, index) in passPatientInfos" :key="index">
                {{ item.orderNo || '\u00a0' }}&nbsp;{{ item.patientName || '\u00a0' }}
              </div>
            </div>
          </vue-seamless-scroll>
        </template>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from '@/utils/common'
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

export default {
  name: "scene-wait-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  components: {
    CompOperate,
    vueSeamlessScroll
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
    }
  },


  computed: {
    // 标题状态
    headerShown() {
      let {headerShown = true} = (this.optionsInfo);
      return headerShown
    },

    // 过号患者信息
    passPatientInfos() {
      let devType = (this.devType);
      let waitingPatientAllList = this.$store.getters['getWaitPatientAllListInfo'];
      if (devType) {
        // let dataInfo = waitingPatientAllList.apiData;
        // console.log(this.$lodash.sortBy(dataInfo, 'orderNo'))
        return waitingPatientAllList.apiData;
      } else {
        // let dataInfo = waitingPatientAllList.mockData
        // console.log(this.$lodash.sortBy(dataInfo, 'orderNo'))
        return waitingPatientAllList.mockData;
      }
    },

    // 温馨提示的滚动
    classOptionTips() {
      let {scrollableNumber} = (this.optionsInfo)
      return {
        step: [1, 1.5, 2, 2.5, 3][0], // 数值越大速度滚动越快
        hoverStop: true,
        limitMoveNum: scrollableNumber, // 开始无缝滚动的数据量 this.dataList.length
        direction: 2 // 0向下 1向上 2向左 3向右
      };
    },


    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'wrap':
            if (!!info.backgroundColor) {
              Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);

            } else {
              Reflect.deleteProperty(Obj, 'backgroundColor');
            }
            Reflect.set(Obj, 'height', pxToVw(info.contentHeight));
            Reflect.set(Obj, 'margin', `${pxToVw(info.marginVertical)} ${pxToVw(info.marginHorizontal)}`);
            return Obj;
          case 'head':
            if (!!info.backgroundColor) {
              Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
              Reflect.set(Obj, 'fontWeight', `${(info.headerFontWeight)}`);
            } else {
              Reflect.deleteProperty(Obj, 'backgroundColor');
            }
            Reflect.set(Obj, 'fontSize', pxToVw(info.headerFontSize));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.headerBorderRadius));
            Reflect.set(Obj, 'color', `${(info.headerFontColor)}`);
            Reflect.set(Obj, 'backgroundColor', `${(info.headerBackgroundColor)}`);
            return Obj;
          case 'list':
            Reflect.set(Obj, 'color', `${(info.listFontsColor)}`);
            Reflect.set(Obj, 'fontSize', pxToVw(info.listFontsize));
            Reflect.set(Obj, 'fontWeight', `${(info.listFontWeight)}`);
            return Obj;
        }
      }
    },
  },

  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop 需要在父组件created中初始化！！ */
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    handleEnd() {
      this.widgetKey = uuid();
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "waitPatientListInfo":
          this.widgetKey = uuid();
          this.$store.dispatch('setWaitPatientAllInfo', this.$lodash.cloneDeep(data))
          break
      }
    }
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = val.options.config;
        this.widgetKey = uuid();
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    },
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-wait-widget {
  display: flex;
  color: #333;
  overflow: hidden;
  font-size: 32px;
  margin: 10px 28px 16px 28px;
  box-sizing: border-box;
  background: transparent;

  .left {
    white-space: nowrap;
    border-radius: 6px;;
    padding: 12px 46px;
    color: white;
    display: flex;
    justify-content: center;
    align-self: center;
    align-items: center;
    background: #FFA72C;
    margin-right: 32px;
    max-height: 50px;
    max-width: 118px;
    overflow: hidden;
    flex-shrink: 0;
  }

  .right {
    display: flex;
    flex-direction: row;
    overflow: hidden;
    padding: 8px 18px;
    cursor: pointer;

    .scroll {
      display: flex;
      align-items: center;
      align-self: center;

      :deep(div) {
        display: flex;

        .inside-content {
          display: flex;
          font-size: 54px;

          .patient {
            white-space: nowrap;
            padding-right: 38px;
          }
        }
      }
    }

  }
}
</style>
